package com.rcadmin.busi.logview.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.rcadmin.busi.logview.dao.ProjectDao;
import com.rcadmin.busi.logview.dao.ProjectRefResourceDao;
import com.rcadmin.busi.logview.dao.ResourceDao;
import com.rcadmin.busi.logview.ddl.ProjectDO;
import com.rcadmin.busi.logview.ddl.ProjectRefResourceDO;
import com.rcadmin.busi.logview.ddl.ResourceDO;
import com.rcadmin.common.bean.TreeObject;
import com.rcadmin.common.exception.BusiException;
import com.rcadmin.common.prop.LogViewProp;
import com.rcadmin.core.jpa.DaoException;

/**
 * 资源分类服务类
 * 
 * @author tanson lam
 * @creation 2015年5月13日
 */
@Component
public class ResourceService {
	@Resource
	private ResourceDao resourceDao;
	@Resource
	private ProjectRefResourceDao projectRefResourceDao;
	@Resource
	private ProjectDao projectDao;
	@Resource
	private LogViewProp logViewProp;

	/**
	 * 查看项目的服务器资源
	 * 
	 * @param projectId
	 * @return
	 * @throws DaoException
	 */
	public List<ResourceDO> findResourceByPrjId(Integer projectId)
			throws DaoException {
		List<ProjectRefResourceDO> refList = projectRefResourceDao
				.findByPrjId(projectId);
		final List<Integer> idsList = Lists.transform(refList,
				new Function<ProjectRefResourceDO, Integer>() {
					@Override
					public Integer apply(
							ProjectRefResourceDO projectRefResourceDO) {
						if (projectRefResourceDO == null)
							return null;
						return projectRefResourceDO.getId();
					}
				});
		return resourceDao.findResourceByIds(idsList
				.toArray(new Integer[idsList.size()]));
	}

	public List<TreeObject> getProjectFileTree(Integer projectId)
			throws IOException, BusiException {
		ProjectDO projectDO = projectDao.findById(projectId);
		if (projectDO == null)
			throw new BusiException("没有对应的项目！");
		List<TreeObject> fileTreeList = new ArrayList<TreeObject>();
		LogPathUtil.loadFileTree(logViewProp.getDir(),
				projectDO.getName(),
				new File(logViewProp.getDir() + File.separator
						+ projectDO.getCode()), "0", fileTreeList, 0);
		return fileTreeList;
	}

	/**
	 * 查看项目的服务器资源
	 * 
	 * @param projectId
	 * @throws DaoException
	 * @throws BusiException
	 */
	public List<TreeObject> mountProjectTreeList(Integer projectId)
			throws BusiException {
		if (projectId == null)
			throw new BusiException("项目ID不能为空！");
		List<ResourceDO> serverList = findResourceByPrjId(projectId);
		List<TreeObject> serverTreeList = new ArrayList<TreeObject>();
		ProjectDO projectDO = projectDao.findById(projectId);
		if (projectDO == null)
			throw new BusiException("没有对应的项目！");
		TreeObject rootTreeObject = new TreeObject("0", "-1",
				projectDO.getName(), true, "");
		serverTreeList.add(rootTreeObject);
		for (ResourceDO server : serverList) {
			TreeObject dto = new TreeObject(server.getId().toString(), 0 + "",
					server.getServerAddr(), true, "");
			serverTreeList.add(dto);
		}
		return serverTreeList;
	}

	private void listLogFile2Server(ResourceDO server) {

	}

	private void addLogFileByMock(Integer serverId,
			List<TreeObject> serverTreeList) {
		if (serverId == null || serverTreeList == null)
			return;
		TreeObject dto1 = new TreeObject(serverId + "_" + "11", serverId + "",
				"access.log", true, "");
		TreeObject dto2 = new TreeObject(serverId + "_" + "22", serverId + "",
				"access-2016-09-05.log", true, "");
		serverTreeList.add(dto1);
		serverTreeList.add(dto2);
	}
}
